<?php


class module{
    public $cfg;//setted from global $load
    public $view_layout = "";
    public $view_tpl = "";
    public $base_url;//setted from global $load
    public $path; //setted from global $load
    public $lib;
    public function __construct($cfg = null,$base_url = null,$path = null) {
        global $load;
        global $mods;

        $this->cfg = $load->cfg;
        $this->base_url = $load->base_url;
        $this->path = $load->path;
        $this->mods = $mods;
        
        $this->statics = $this->base_url.$this->cfg["statics"];
    }
    function load_lib($lib_name = null){
        @$this->lib->$lib_name = $this->load_and_instance('../lib/',$lib_name,'.lib');
    }
    function load_mod($module_name = null){
        @$this->mod->$module_name = $this->load_and_instance('../mod/'.$module_name.'/',$module_name,'.mod'); 
    }
    
    
    function load_and_instance($folder_name,$class_name,$ext){
        $path = $folder_name.$class_name.$ext.'.php';
        if(file_exists($path)){
            include_once ($path);
            $instance = new $class_name();
            return $instance;
        }else{
            echo ("No se encuentra el modulo o liberia $class_name");
        }
    }
    
    
    function load_layout(){
        if($this->view_layout != ""){
            $path = '../views/layouts/'.$this->view_layout.'.layout.php';
            if(file_exists($path)){ include_once $path; }
        }
    }
    function load_tpl($filename){
        if($this->view_tpl != ""){
            $path = '../views/tpls/'.$filename.'.tpl.php';
            if(file_exists($path)){ include_once $path; }
        }
    }
    function load_content(){
        if(isset($this->view_tpl)){        
            $this->load_tpl($this->view_tpl);
        }
    }
    function redirect($to){
         header("Location: ". $this->base_url.$to);
    }
    function base_url($url=null,$ret=null){
        if($ret){
            return $this->base_url.$url;
        }else{
            echo $this->base_url.$url;
        }
    }
}
?>
